package com.charlessong.unionpay.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import lombok.experimental.UtilityClass;

import java.util.UUID;

/**
 * @author charles
 * @Date 2021/11/18
 */
@UtilityClass
public class PayUtils {
    private static String APP_PRIVATE_KEY="MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCZRdCJNa0wHA0tPpeJXwbReYEwexYe6e0jPHGMMesp3AM2hazFw7mjuMfJk3mnacEit1ZvTnh5tkk0yiX7tnpDzwvR1JqclCgL+1mbAi/Qsd5SPJT6PV5gSHe2e+JgGzgc53qYEhc3RS9XrTyUd+nt0Y/N/oOp50sTfkjac1jaf8davj1Q/QV3mnDKRbrl0bTJBYeCWpnXPx3WJ9w8M8UeaGV0INy9XxVMAg26Yaz3DrWCwVMpVIIiDQo/Ud8Cialw1mAzZ9Y31ByhFZ0A6nX4gbjOrIEZVz7XJEcz0SHFz3VSFfFxs7lYkqylEsEjC3t1Ln0PZE3LsFPq9kWMAtw3AgMBAAECggEBAIcEQ/4KZaSCoWzgvVaKehQe55nQY50ZzgaDITcxZgm6wK3PtXGEx8wcYwoDul+qPjMZs69bPa9hzz0Mccc5aJzBD5VopkYWEOz1Hmr40ywQ81oX+dgyJSfFzobWvISSlayIkKrTDNZoQJsIhNv4gf4342i/sjYCBFWIoh6lC3l1RyRNSgOjkjii8ri2yig8taCkccIActZzwIbOUpHK8Xnx1ORh6TuP6e0FEJUJJAw35o2MI+jaFaMcKs+yLKWxjv9VIbW2CGGMD3QNVT9w3vrjRQKtDpaMjsYTHkpDo1daqeueXxsUipzAGEgEtRI8cpaV+eIoGeGWsN/bJ3U6atECgYEAx7odtGyihStig6tVTijthZ3CknBZ2WeCoeAiagWA8rrO1fxL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8CqsF2T5IH8SZj5Y7FUJWKIGF3kEk2q9Um0UfkUAK/lW1OwX2vOXi/kiAJOyoZhbr5tnDesV7TopByPNNgM9iKUXcXX45zt+f+ksj0KQsYj31Rz3LL4tV83nRNsHIs9rXDHYlGOgKf1W85qHRBx6ZXFYSKq2+F213nC3Ggb4Fwe5qbJfSOWRyTu6QIDAQAB";
    public JSONObject aliPay(String sign) {
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", "2021000118652119", APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody("我是测试数据");
        model.setSubject("App支付测试Java");
        model.setOutTradeNo(UUID.randomUUID().toString());
        model.setTimeoutExpress("30m");
        model.setTotalAmount("0.01");
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl("https://wx.xsmapp.com/unionpay/notify/alipay");
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            System.out.println(response.getBody());//就是orderString 可以直接给客户端请求，无需再做处理。
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }
}
